package com.dji.sample.common.util;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author yjt
 * @date 2022-10-27
 * @description:
 */
public class DistanceUtil {

    public static void main(String[] args) {
        System.out.println("经纬度距离计算结果：" + getDistance(102.840529,24.887657, 102.868125,24.87625) + "米");
    }


    public static double getDistance(double longitudeFrom, double latitudeFrom, double longitudeTo, double latitudeTo) {
        GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
        GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);
        double b = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target).getEllipsoidalDistance();
        BigDecimal bigDecimal = new BigDecimal(b);
        bigDecimal=bigDecimal.setScale(2, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }
}
